from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.viewsets import ModelViewSet

from .models import Lybs
from .serializers import LybsSerialzier


class PageNumber(PageNumberPagination):
    """
    自定义分页器
    """
    # 指定控制每页数量的参数
    page_size_query_param = 'page_size'
    # 指定每页最大返回数量
    max_page_size = 10


class LybsView(ModelViewSet):
    # 指定当前类视图使用的查询集数据
    queryset = Lybs.objects.all().order_by('id')
    # 指定当前类视图使用的系列化器
    serializer_class = LybsSerialzier
    # 指定分页器
    pagination_class = PageNumber
    # DjangoFilterBackend对应filter_fields属性，做单字段相等搜索
    # SearchFilter对应search_fields，多字段模糊搜索
    filter_backends = [DjangoFilterBackend, SearchFilter]
    # 指定过滤的字段
    filter_fields = ('title', 'content')
    # 搜索字段
    search_fields = ['title']
